package com.yequan.thread.keyword.volatileword;

/**
 * @author : Administrator
 * @date : 2020/5/17
 */
public class VolatileExample {

    int a = 0;
    volatile boolean flag = false;

    public void writer() {
        a = 1;
        flag = true;
    }

    public void reader() {
        if (flag) {
            int i = a;
            System.out.println(i);
        } else {
            System.out.println("---------------");
        }
    }

    public static void main(String[] args) {
        VolatileExample volatileExample = new VolatileExample();
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> volatileExample.writer()).start();
            new Thread(() -> volatileExample.reader()).start();
        }
    }

}
